//
//  RBIntegralDetailVC.m
//  zhongzhenglu
//
//  Created by 马军凯 on 2023/5/4.
//

#import "RBIntegralDetailVC.h"
#import "RBIntegralDetailResponse.h"
#import "RBIntegralDetailCell.h"

@interface RBIntegralDetailVC ()
<UITableViewDataSource, UITableViewDelegate>
//tableView
@property (nonatomic, strong) UITableView *tableView;
//
@property (nonatomic, retain) NSMutableArray *dataSource;

//
@property (nonatomic, strong) RBIntegralDetailResponse *response;

@end

@implementation RBIntegralDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setup];
    [self.listView.mj_header beginRefreshing];
}
- (void)setup {
    self.title = @"积分明细";
    self.view.backgroundColor = [RBThemeConfig bgGrayColor];
}

- (void)fillData {
    [self.dataSource removeAllObjects];
    [self.dataSource addObject:({
        RBGroupModel *group = RBGroupModel.new;
        group.cellModels = ({
            NSMutableArray *arrayM = NSMutableArray.array;
            arrayM;
        });
        group;
    })];
    [self appendData];
}
- (void)appendData {
    NSMutableArray *arrayM = ({
        RBGroupModel *group = self.dataSource.lastObject;
        (NSMutableArray *)group.cellModels;
    });
    for (RBIntegralDetailModel *model in self.response.data) {
        [arrayM addObject:({
            RBCellModel *cellModel = RBCellModel.new;
            cellModel.reuseCellIdentifier = NSStringFromClass(RBIntegralDetailCell.class);
            cellModel.cellData = model;
            cellModel;
        })];
    }
    [self.tableView reloadData];
    if(!arrayM.count)
    {
        [self.tableView showPlaceHolder:RB_EMPTY_TIP tapBlock:nil];
    }
}
#pragma mark - Network

- (NSString *)listPageKey
{
    return @"page";
}
- (NSDictionary *)listParams
{
    NSMutableDictionary *dicM = NSMutableDictionary.dictionary;
    dicM[@"user_id"] = [RBAccountManager getUserId];
    return dicM;
}
- (NSString *)listRequestPath
{
    return @"api/integral_log";
}
- (NSInteger)ListCurrentPage
{
    return self.response.currentPage;
}
- (BOOL)listLog
{
    return YES;
}
- (BOOL)listPost
{
    return YES;
}
- (Class)listResponseClass
{
    return RBIntegralDetailResponse.class;
}
- (UIScrollView *)listView
{
    return self.tableView;
}

- (void)getFirstPageData
{
    __weak typeof(self) weak_self = self;
    [self listLoadFirstPageWithDesView:self.listView showPlaceHolder:YES sucBlock:^(RBIntegralDetailResponse *data) {
        weak_self.response = data;
        [weak_self fillData];
    } failBlock:^(id data) {
        
    } reloadBlock:^{
        [weak_self getFirstPageData];
    }];
}

- (void)getNextPageData
{
    __weak typeof(self) weak_self = self;
    [self listLoadNextPageWithSucBlock:^(RBIntegralDetailResponse *data) {
        weak_self.response = data;
        [weak_self appendData];
    } failBlock:^(id data) {
        
    }];
}

#pragma mark - UITableViewDataSource & Delegate

kRBTableViewDataSourceAndDelegate(self.dataSource);

#pragma mark - Event

#pragma mark - Setter

#pragma mark - Getter

- (UITableView *)tableView
{
    if (!_tableView)
    {
        self.tableView = ({
            UITableView *view = [UITableView.alloc initWithFrame:CGRectZero style:UITableViewStylePlain];
            view.dataSource = self;
            view.delegate = self;
            view.showsVerticalScrollIndicator = NO;
            view.showsHorizontalScrollIndicator = NO;
            view.estimatedRowHeight = UITableViewAutomaticDimension;
            kRBTableViewCommentAdjust(view);
            view.separatorStyle = UITableViewCellSeparatorStyleNone;
            view.backgroundColor = [RBThemeConfig bgGrayColor];
            UIView *footView = [UIView new];
            footView.frame = CGRectMake(0, 0, SCREEN_WIDTH, TAB_BAR_SAFE_MARGIN);
            view.tableFooterView = footView;
            view.mj_header = [RBRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(getFirstPageData)];
            view.mj_footer = [RBRefreshFooter footerWithRefreshingTarget:self refreshingAction:@selector(getNextPageData)];
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.top.right.bottom.mas_equalTo(0);
            }];
            view;
        });
    }
    return _tableView;
}

- (NSMutableArray *)dataSource
{
    if (!_dataSource)
    {
        self.dataSource = NSMutableArray.array;
    }
    return _dataSource;
}


@end
